<script>
import { getCurrentInstance } from "vue";
import { useMyToastStore } from "@/store/myToast";
export default {
  onLaunch: async () => {
    const myToast = useMyToastStore();
    const instance = getCurrentInstance();
    const util = instance?.proxy?.$util;
    const request = instance?.proxy?.$request;
    console.log("App Launch");
    // 获取信息
    let todayDate = uni.$u.timeFormat(Number(new Date()), "yyyy/mm/dd");
    let newLoginDate = uni.getStorageSync("newLoginDate") || "";
    let userInfo = uni.getStorageSync("userInfo") || "";
    if (!userInfo) {
      return;
    }
    console.log(todayDate, newLoginDate);
    if (todayDate != newLoginDate) {
      setTimeout(() => {
        myToast.params = {
          type: "warning",
          message: "检查登录状态...",
        };
      }, 500);
      try {
        // 获取登录信息
        const loginData = await util.getPublicKey(userInfo.password);
        console.log(loginData);
        // 登录
        const result = await request.post("login", {
          password: loginData.encrypt,
          account: userInfo.account,
          csrftoken: loginData.csrftoken,
          cookie: loginData.cookie,
        });
        if (result.data) {
          uni.setStorageSync("userInfo", {
            cookie: result.data,
            account: userInfo.account,
            password: userInfo.password,
          });
          uni.setStorageSync("loginExpired", true);
          uni.setStorageSync("newLoginDate", todayDate);
          uni.$emit("updateLoginInfo");
          myToast.params = {
            type: "success",
            message: "登录成功",
          };
        } else {
          myToast.params = {
            type: "error",
            message: "登录失败，请手动重新登录",
          };
        }
      } catch (error) {
        console.log(error);
        myToast.params = {
          type: "error",
          message: "登录出现问题，请稍后重试",
        };
      }
    }
  },
  onShow: function () {
    console.log("App Show");
  },
  onHide: function () {
    console.log("App Hide");
  },
};
</script>

<style lang="scss">
/*每个页面公共css */
@import "uview-plus/index.scss";
@import "@/styles/global.scss";
</style>
